﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.ModLoader;

namespace MoLingsArmory.Content.Projectiles
{
    public class NebulaNova : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.TrailCacheLength[Projectile.type] = 7;   
            ProjectileID.Sets.TrailingMode[Projectile.type] = 0;       
        }

        public override bool PreDraw(ref Color lightColor)
        {
            Vector2 drawOrigin = new(TextureAssets.Projectile[Projectile.type].Value.Width * 0.5f, Projectile.height * 0.5f);
            for (int k = 0; k < Projectile.oldPos.Length; k++)
            {
                Vector2 drawPos = Projectile.oldPos[k] - Main.screenPosition + drawOrigin + new Vector2(0f, Projectile.gfxOffY);
                Color color = Projectile.GetAlpha(lightColor) * ((Projectile.oldPos.Length - k) / (float)Projectile.oldPos.Length);
                Main.spriteBatch.Draw(TextureAssets.Projectile[Projectile.type].Value, drawPos, null, color, Projectile.rotation, drawOrigin, Projectile.scale, SpriteEffects.None, 0f);
            }
            return true;
        }

        public override Color? GetAlpha(Color lightColor)
        {
            return new Color(255 - Projectile.alpha, 255 - Projectile.alpha, 255 - Projectile.alpha, 64 - Projectile.alpha / 4);
        }

        public override void SetDefaults()
        {
            Projectile.width = 18;
            Projectile.height = 18;
            Projectile.friendly = true;
            Projectile.penetrate = 3;
            Projectile.extraUpdates = 1;
            Projectile.timeLeft = 400;
            Projectile.alpha = 255;
            Projectile.ignoreWater = true;
            Projectile.DamageType = DamageClass.Magic;
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCImmunity[target.whoAmI] = 10;
            target.immune[Projectile.owner] = 0;        
        }
        public override void AI()
        {
            Lighting.AddLight(Projectile.position, 1f, 0.5f, 0.9f);
            Projectile.rotation = (float)Math.Atan2(Projectile.velocity.Y, Projectile.velocity.X) + 1.57f;
            if (Main.rand.NextBool(4))
            {
                int num765 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, DustID.PinkTorch, 0f, 0f, 100, default, 1.5f);
                Main.dust[num765].noGravity = true;
                Main.dust[num765].velocity *= 0f;
            }

            if (Projectile.ai[0] > 5f && Projectile.alpha > 0)
            {
                Projectile.alpha -= 15;
            }
            else if (Projectile.alpha > 0)
            {
                Projectile.ai[0]++;
            }
        }

        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item10, Projectile.position);
            for (int i = 0; i < 12; i++)
            {
                int num274 = Dust.NewDust(Projectile.Center, 0, 0, DustID.PinkTorch, 0f, 0f, 100, default, 1.7f);
                Dust dust = Main.dust[num274];
                dust.velocity *= 6f;
                Dust dust28 = Main.dust[num274];
                dust28.velocity.Y = dust28.velocity.Y - 1f;
                Main.dust[num274].position = Vector2.Lerp(Main.dust[num274].position, Projectile.Center, 0.5f);
                Main.dust[num274].noGravity = true;
            }
        }
    }
}
